﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// BasicClass 公共常用类
/// </summary>
public class BasicClass
{
    private string _tablePrefix = "DATA";
	public BasicClass()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    //产生一个Hashtable
    public Hashtable CreateHashtable(int fromNumber, int toNumber)
    {
        Hashtable returnHt = new Hashtable();
        for (int i = fromNumber; i <= toNumber; i++)
        {
            returnHt.Add(i, i);
        }
        return returnHt;
    }
    //产生一个关于文件相关数据表前缀字符串的hashtable
    public DataSet GetTablePrefixHashTable()
    {
        string sqlString = "select * from FLOWTYPE order by FLOWTYPEID asc";
        DBOperation DBO = new DBOperation();
        DataSet ds = DBO.GetDataSet(sqlString,"temp");
        return ds;
        //Hashtable TablePrefix = new Hashtable();
        
        //TablePrefix["JCZL"] = "基础资料";
        //TablePrefix["ZBZL"] = "指标资料";
        //TablePrefix["ZBPL"] = "指标计划";
        //TablePrefix["FXBG"] = "分析报告";
        //TablePrefix["common"] = "普通文件"; 
        //return TablePrefix;
    }
    //产生一个资料前缀关联数据表前缀字符串的hashtable
    public static Hashtable GetTabPrefixAndDataIDHashTab()
    {
        Hashtable TabPrefixDataID = new Hashtable();
        TabPrefixDataID["JC"] = "JCZL";
        TabPrefixDataID["FX"] = "FXBG";
        TabPrefixDataID["JL"] = "JLCB";
        TabPrefixDataID["KH"] = "KHZL";
        TabPrefixDataID["FW"] = "FFWC";
        TabPrefixDataID["LJ"] = "LLJS";
        TabPrefixDataID["ZW"] = "ZBWC";
        TabPrefixDataID["XJ"] = "XJYY";
        return TabPrefixDataID;
    }
    //根据用户名获取部门名称
    public string GetDepNameByUserName(string userName)
    {
        string sqlString = "select DEPARTMENT.DEPNAME as DEPNAME from DEPARTMENT inner join USERS on USERS.DEPID=DEPARTMENT.DEPID where USERS.USERNAME='" + userName + "'";
        DBOperation DBO = new DBOperation();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        string returnDepName = "";
        if (sdr.Read())
        {
            returnDepName = sdr["DEPNAME"].ToString();

        }
        else
        {
            returnDepName = null;
        }
        sdr.Close();
        return returnDepName;
        

    }
    //根据部门ID获取部门名称
    public static string GetDepNameByDepID(string DepID)
    {
        string DepName = "";
        if (!(DepID == "" || DepID == "0"))
        {
            string sqlString = "select DEPNAME from DEPARTMENT  where DEPID=" + DepID;
            DBOperation DBO = new DBOperation();
            DepName = DBO.ExcuteScalar(sqlString).ToString();
        }        
        return DepName;
    }
    //根据资料ID获取资料的上传部门名称
    public static string GetDepNameByDataID(string DataID)
    {        
        Hashtable getTableName = GetTabPrefixAndDataIDHashTab();
        string dataIdPrefix = DataID.Substring(0, 2);
        string dataYear = DataID.Substring(2, 4);
        string tabPrefix = "DATA";
        //string tabPrefix = getTableName[dataIdPrefix].ToString();
        string tablename = tabPrefix + dataYear;
        string sqlString = "select DEP.DEPNAME from DEPARTMENT as DEP inner join " + tablename + " as dataTab on dataTab.DEPID=DEP.DEPID where dataTab.DATAID='" + DataID + "'";
        DBOperation DBO = new DBOperation();
        string depName = DBO.ExcuteScalar(sqlString).ToString();
        return depName; 
    }
    //根据资料编号获取资料标题
    public static string GetDataTitleByDataID(string DataID)
    {
        Hashtable getTableName = GetTabPrefixAndDataIDHashTab();
        string dataIdPrefix = DataID.Substring(0, 2);
        string dataYear = DataID.Substring(2, 4);
        string tabPrefix = "DATA";
        //string tabPrefix = getTableName[dataIdPrefix].ToString();
        string tablename = tabPrefix + dataYear;
        string sqlString = "select title from " + tablename + " where DATAID='" + DataID + "'";
        DBOperation DBO = new DBOperation();
        string title = DBO.ExcuteScalar(sqlString).ToString();
        return title;
    }

    //根据用户名获取该用户所在部门ID
    public string GetDepIDByUserName(string userName)
    {
        string sqlString = "select DEPARTMENT.DEPID as DEPID from DEPARTMENT inner join USERS on USERS.DEPID=DEPARTMENT.DEPID where USERS.USERNAME='" + userName + "'";
        DBOperation DBO = new DBOperation();
        string DepID = null;
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        if (sdr.Read())
        {
            DepID= sdr["DEPID"].ToString();
        }        
        sdr.Close();
        return DepID;

    }

    //根据用户名获取真实姓名
    public static string GetTrueNameByUserName(string userName)
    {
        string sqlString = "select TRUENAME from USERS where USERNAME='" + userName + "'";
        DBOperation DBO = new DBOperation();
        string trueName = null;
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        if (sdr.Read())
        {
            trueName = sdr["TRUENAME"].ToString();
        }       
        sdr.Close();
        return trueName;
    }
    //根据状态编号获取状态类型
    public static string GetDataStatusNameByStatusID(string statusID)
    {
        string statusString = "";
        if (statusID == "1")
        {
            statusString = "已完成";
        }
        else
        {
            statusString = "<span class='notCheck'>审核中...</span>";
        }
        return statusString;

    }
    /// <summary>
    /// 根据sql语句判断某条记录是否存在
    /// </summary>
    /// <param name="sqlString">sql语句</param>
    /// <returns>假如存在则返回true，否则返回false</returns>
    public Boolean CheckRecordExist(string sqlString)
    {
        DBOperation DBO = new DBOperation();
        int count = Convert.ToInt32(DBO.ExcuteScalar(sqlString));
        if (count == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    /// <summary>
    /// 根据异常编号获取异常名称
    /// </summary>
    /// <param name="YCID"></param>
    /// <returns></returns>
    public static string GetYCTitleByYCID(string YCID)
    {
        string dataYear = YCID.Substring(2, 4);
        string tableName = "YCMS" + dataYear;
        string sqlString = "select TITLE from " + tableName + " where YCID='" + YCID + "'";
        string YCTitle = "";
        DBOperation DBO = new DBOperation();
        try
        {
            YCTitle = DBO.ExcuteScalar(sqlString).ToString();
        }
        catch (Exception ex)
        {
            string exMsg = ex.Message.ToString();
            YCTitle = "";
        }
        return YCTitle; 
    }
    /// <summary>
    /// 根据角色ID获取角色名称
    /// </summary>
    /// <param name="roleID">角色ID</param>
    /// <returns>返回角色名称</returns>
    public static string GetRoleNameByRoleID(int roleID)
    {
        string sqlString = "select ROLENAME from ROLE where ROLEID=" + roleID + "";
        DBOperation DBO = new DBOperation();
        string roleName = "";
        object temp = DBO.ExcuteScalar(sqlString);
        if (temp != null)
        {
            roleName = temp.ToString();
        }
        else
        {
            roleName = "未知用户";
        }
        return roleName; 
    }
    /// <summary>
    /// 根据资料编号获取流程状态
    /// </summary>
    /// <param name="DataID">资料编号</param>
    /// <returns>返回流程状态代号</returns>

    public static string GetFlowStatusByDataID(string DataID)
    {
        string dataYear=DataID.Substring(2,4);
        string tabName="LC"+dataYear;
        string sqlString = "select STATUS from " + tabName + " where DATAID='" + DataID + "'";
        string status = "";
        DBOperation DBO = new DBOperation();
        status = DBO.ExcuteScalar(sqlString).ToString();
        return status;
    }
    /// <summary>
    /// 根据状态代号判断信息时候已读，返回中文说明
    /// </summary>
    /// <param name="statusID">状态代号</param>
    /// <returns>当代号为1返回“已读",否则返回“未读”</returns>
    public static string GetLogStatusByStatusID(string statusID)
    {
        string statusString = "";
        if (statusID == "1")
        {
            statusString = "已读";
        }
        else
        {
            statusString = "<span class='notCheck'>未读</span>";
        }
        return statusString;
    }
    /// <summary>
    /// 根据类型ID获得类型名称
    /// </summary>
    /// <param name="typeId">类型ID</param>
    /// <returns>类型名称</returns>
    public static string GetFlowTypeNameByTypeId(int typeId)
    {
        string sqlString = "select TYPENAME from FLOWTYPE where FLOWTYPEID=" + typeId + "";
        DBOperation DBO = new DBOperation();
        string reTypeName = DBO.ExcuteScalar(sqlString).ToString();        
        return reTypeName;
    }
    /// <summary>
    /// 返回表示资料上传菜单的二维数组
    /// </summary>
    /// <returns>返回表示资料上传菜单的二维数组</returns>
    public string[,] GetUpDataMenu()
    {
        string[,] upDataMenu = new string[9, 10];
        //基础资料
        upDataMenu[0, 0] = "线损基础资料提交";
        upDataMenu[0, 1] = "撰  写  人";
        upDataMenu[0, 2] = "主  文  件";
        upDataMenu[0, 3] = "附      件";
        upDataMenu[0, 4] = "标      题";
        upDataMenu[0, 5] = "审 核 流 程";
        upDataMenu[0, 6] = "送交至";
        upDataMenu[0, 7] ="撰写时间";
        upDataMenu[0, 8] ="资料简介";
        upDataMenu[0, 9] = "副标题";
        //分析报告
        upDataMenu[1, 0] = "线损分析报告提交";
        upDataMenu[1, 1] = "撰  写  人";
        upDataMenu[1, 2] = "主  文  件";
        upDataMenu[1, 3] = "附      件";
        upDataMenu[1, 4] = "标      题";
        upDataMenu[1, 5] = "审 核 流 程";
        upDataMenu[1, 6] = "送交至";
        upDataMenu[1, 7] = "撰写时间";
        upDataMenu[1, 8] = "分析报告简介";
        upDataMenu[1, 9] = "副标题";
        //计量与抄表
        upDataMenu[2, 0] = "计量与抄表资料提交";
        upDataMenu[2, 1] = "撰  写  人";
        upDataMenu[2, 2] = "主  文  件";
        upDataMenu[2, 3] = "附      件";
        upDataMenu[2, 4] = "标      题";
        upDataMenu[2, 5] = "审 核 流 程";
        upDataMenu[2, 6] = "送交至";
        upDataMenu[2, 7] = "撰写时间";
        upDataMenu[2, 8] = "资料简介";
        upDataMenu[2, 9] = "副标题";
        //考核资料
        upDataMenu[3, 0] = "考核资料提交";
        upDataMenu[3, 1] = "撰  写  人";
        upDataMenu[3, 2] = "主  文  件";
        upDataMenu[3, 3] = "附      件";
        upDataMenu[3, 4] = "标      题";
        upDataMenu[3, 5] = "审 核 流 程";
        upDataMenu[3, 6] = "送交至";
        upDataMenu[3, 7] = "撰写时间";
        upDataMenu[3, 8] = "资料简介";
        upDataMenu[3, 9] = "副标题";
        //分线、分台区完成资料
        upDataMenu[4, 0] = "分线、分台区完成情况资料提交";
        upDataMenu[4, 1] = "撰  写  人";
        upDataMenu[4, 2] = "主  文  件";
        upDataMenu[4, 3] = "附      件";
        upDataMenu[4, 4] = "标      题";
        upDataMenu[4, 5] = "审 核 流 程";
        upDataMenu[4, 6] = "送交至";
        upDataMenu[4, 7] = "撰写时间";
        upDataMenu[4, 8] = "资料简介";
        upDataMenu[4, 9] = "副标题";
        //理论计算资料
        upDataMenu[5, 0] = "理论计算开展情况资料提交";
        upDataMenu[5, 1] = "撰  写  人";
        upDataMenu[5, 2] = "主  文  件";
        upDataMenu[5, 3] = "附      件";
        upDataMenu[5, 4] = "标      题";
        upDataMenu[5, 5] = "审 核 流 程";
        upDataMenu[5, 6] = "送交至";
        upDataMenu[5, 7] = "撰写时间";
        upDataMenu[5, 8] = "资料简介";
        upDataMenu[5, 9] = "副标题";
        //指标完成资料
        upDataMenu[6, 0] = "线损率指标完成资料提交";
        upDataMenu[6, 1] = "撰  写  人";
        upDataMenu[6, 2] = "主  文  件";
        upDataMenu[6, 3] = "附      件";
        upDataMenu[6, 4] = "标      题";
        upDataMenu[6, 5] = "审 核 流 程";
        upDataMenu[6, 6] = "送交至";
        upDataMenu[6, 7] = "撰写时间";
        upDataMenu[6, 8] = "资料简介";
        upDataMenu[6, 9] = "副标题";
        //新技术应用与管理创新
        upDataMenu[7, 0] = "新技术应用与管理创新资料提交";
        upDataMenu[7, 1] = "撰  写  人";
        upDataMenu[7, 2] = "主  文  件";
        upDataMenu[7, 3] = "附      件";
        upDataMenu[7, 4] = "标      题";
        upDataMenu[7, 5] = "审 核 流 程";
        upDataMenu[7, 6] = "送交至";
        upDataMenu[7, 7] = "撰写时间";
        upDataMenu[7, 8] = "资料简介";
        upDataMenu[7, 9] = "副标题";
        //线损四分管理必备内容
        upDataMenu[8, 0] = "线损四分管理必备内容达标资料提交";
        upDataMenu[8, 1] = "撰  写  人";
        upDataMenu[8, 2] = "主  文  件";
        upDataMenu[8, 3] = "附      件";
        upDataMenu[8, 4] = "标      题";
        upDataMenu[8, 5] = "审 核 流 程";
        upDataMenu[8, 6] = "送交至";
        upDataMenu[8, 7] = "撰写时间";
        upDataMenu[8, 8] = "资料简介";
        upDataMenu[8, 9] = "副标题";
        return upDataMenu;
    }
    /// <summary>
    /// 返回表示资料类型缩写和菜单数组第一位下标的关系的Hashtable
    /// </summary>
    /// <returns>返回表示资料类型缩写和菜单数组第一位下标的关系的Hashtable</returns>
    public Hashtable GetArrayIndexByShortMark()
    {
        Hashtable tempHt=new Hashtable();
        tempHt["JC"]=0;
        tempHt["FX"]=1;
        tempHt["JL"]=2;
        tempHt["KH"]=3;
        tempHt["FW"]=4;
        tempHt["LJ"]=5;
        tempHt["ZW"]=6;
        tempHt["XJ"] = 7;
        tempHt["BN"] = 8;
        return tempHt;
    }
    /// <summary>
    /// 根据类型字符缩写获取该类型的类型名称
    /// </summary>
    /// <param name="shortMark">类型字符缩写</param>
    /// <returns>类型名称</returns>
    public string GetFlowTypeNameByShortMark(string shortMark)
    {
        string sqlString = "select TYPENAME from FLOWTYPE where SHORTMARK='" + shortMark + "'";
        DBOperation DBO = new DBOperation();
        string tempTypeName = DBO.ExcuteScalar(sqlString).ToString();
        return tempTypeName;
    }
    /// <summary>
    /// 根据类型字符缩写获取该类型的类型名称:静态方法
    /// </summary>
    /// <param name="shortMark">类型字符缩写</param>
    /// <returns>类型名称</returns>
    public static string GetFlowTypeNameByDataIdStatic(string tempDataId)
    {
        DataOperation DataO = new DataOperation(tempDataId);
        string shortMark = DataO.DataType.ToString();
        string sqlString = "select TYPENAME from FLOWTYPE where SHORTMARK='" + shortMark + "'";
        DBOperation DBO = new DBOperation();
        string tempTypeName = DBO.ExcuteScalar(sqlString).ToString();
        return tempTypeName;
    }

    public Hashtable GetDataYear()
    {
        string sqlString = "select DATAYEAR from DATAYEAR order by DATAYEAR asc";        
        Hashtable tempHt = new Hashtable();        
        DBOperation DBO=new DBOperation();
        SqlDataReader sdr=DBO.GetDataReader(sqlString);
        while(sdr.Read())
        {
            tempHt.Add(sdr["DATAYEAR"].ToString(), sdr["DATAYEAR"].ToString());
        }
        sdr.Close();
        return tempHt;
        
    }
    /// <summary>
    /// 替换html中的特殊字符
    /// </summary>
    /// <param name="theString">需要进行替换的文本。</param>
    /// <returns>替换完的文本。</returns>
    public static string HtmlEncode(string theString)
    {
        theString = theString.Replace(">", "&gt;");
        theString = theString.Replace("<", "&lt;");
        theString = theString.Replace(" ", "&nbsp;");
        theString = theString.Replace(" ", "&nbsp;");
        theString = theString.Replace("\"", "&quot;");
        theString = theString.Replace("\'", "'");
        theString = theString.Replace("\n", "<br/> ");
        return theString;
    }
    /// <summary>
    /// 恢复html中的特殊字符
    /// </summary>
    /// <param name="theString">需要恢复的文本。</param>
    /// <returns>恢复好的文本。</returns>
    public static string HtmlDiscode(string theString)
    {
        theString = theString.Replace("&gt;", ">");
        theString = theString.Replace("&lt;", "<");
        theString = theString.Replace("&nbsp;", " ");
        theString = theString.Replace("&nbsp;", " ");
        theString = theString.Replace("&quot;", "\"");
        theString = theString.Replace("'", "\'");
        theString = theString.Replace("<br/> ", "\n");
        return theString;
    }





}
